import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    history: ["莲子", '石斛', "三七粉", "红枸杞", "百合", "红枸杞"],
    searchJson: [],
    cartList: JSON.parse(window.localStorage.getItem("sanaiCart")) || [],
    check: false
  },
  mutations: {
    delHistory(state) {
      state.history = []
    },
    searchJson(state) {
      axios.get('/data.json').then((resp) => {
        state.searchJson = resp.data
      })
      console.log(state.searchJson)
    },
    addCart(state, m) {
      let fleg = false
      state.cartList.map((item) => {
        if (item.item.id == m.item.id) {
          fleg = true
          item.num++
        }
      })
      if (fleg == false) {
        state.cartList.push(m)
      }
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))

    },
    dd(state) {
      var aa = state.cartList.every((resp) => {
        return resp.check
      })
      if (aa) {
        state.check = true
      } else {
        state.check = false
      }
      window.localStorage.setItem('sanaiCart', JSON.stringify(state.cartList))

    },
    sub(state, m) {
      let index = state.cartList.findIndex((resp) => {
        return resp.item.id == m
      })
      state.cartList[index].num--
      if (state.cartList[index].num == 1) {
        state.cartList[index].num == 1
      }
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    },
    add(state, m) {
      let index = state.cartList.findIndex((resp) => {
        return resp.item.id == m
      })
      state.cartList[index].num++
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    },
    qx(state, m) {
      if (m) {
        state.cartList.map((item) => {
          item.check = m
        })
      }else{
        state.cartList.map((item) => {
          item.check = m
        })
      }

      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    },
    text(state){
      state.cartList.map((item)=>{
        item.check=false
      })
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    },
    textout(state){
      state.cartList.map((item)=>{
        item.check=false
      })
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    },
    dell(state){
      state.cartList.map((item,index)=>{
        if(item.check){
          state.cartList.splice(index,1)
          
        }
      })
      window.localStorage.setItem("sanaiCart", JSON.stringify(state.cartList))
    }
  },
  actions: {
    delHistory(cont) {
      cont.commit("delHistory")
    },
    searchJson(cont) {
      cont.commit("searchJson")
    },
    addCart(cont, m) {
      cont.commit('addCart', m)
    },
    dd(cont, m) {
      cont.commit('dd', m)
    },
    sub(cont, m) {
      cont.commit('sub', m)
    },
    add(cont, m) {
      cont.commit('add', m)
    },
    qx(cont, m) {
      cont.commit('qx', m)
    },
    text(cont){
      cont.commit('text')
    },
    textout(cont){
      cont.commit('textout')
    },
    dell(cont){
      cont.commit('dell')
    }
  },
  modules: {
  }
})
